import 'dart:async';

import 'package:cunyutech_flutter_ui/src/overlay/OverlaySupport.dart';
import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/material.dart';

enum OverlayToastPosition {
  TOP,
  CENTER,
  BOTTOM
}

enum OverlayToastDelay {
  LONG,
  SHORT,
}

void showOverlayToast(
  String msg,
  {
    OverlayToastPosition position,
    OverlayToastDelay delay,
    double fontSize = CYFontSize.Middle,
    Color backgroundColor,
    Color textColor
  }
) async {
  OverlayEntry entry = showOverlay(
    builder: (BuildContext _, OverlaySupportState state) {
      var cs = state.colorSystem;
      var _backgroundColor = backgroundColor ?? Colors.black.withOpacity(0.6);
      var _color = textColor ?? cs.darkForeColorSystem.primaryColor;
      MainAxisAlignment mainAxisAlignment = MainAxisAlignment.center;
      double top = 0;
      double bottom = 0;
      if(position == OverlayToastPosition.TOP) {
        mainAxisAlignment = MainAxisAlignment.start;
        top = 100;
      }
      if(position == OverlayToastPosition.BOTTOM) {
        mainAxisAlignment = MainAxisAlignment.start;
        bottom = 100;
      }
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          Column(
            mainAxisAlignment: mainAxisAlignment,
            children: [
              SizedBox(height: top,),
              Container(
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: CYMargin.Default),
                margin: EdgeInsets.symmetric(horizontal: CYMargin.Default),
                constraints: BoxConstraints(
                  minHeight: 44,
                ),
                decoration: BoxDecoration(
                  color: _backgroundColor,
                  borderRadius: CYBorderRadius.defaultBorderRadius,
                ),
                child: Text(
                  msg,
                  style: TextStyle(
                    color: _color,
                    fontSize: fontSize,
                  ),
                ),
              ),
              SizedBox(height: bottom,),
            ],
          )
        ]
      );
    }
  );
  Duration duration = delay == OverlayToastDelay.LONG ? Duration(seconds: 3) : Duration(seconds: 1);
  await Future.delayed(duration);
  entry.remove();
}
